var flag = 0;
var count = 0;
var brow;
var listUrl = rootPath + '/manage/examPaper/paperCorrectionUI?';
var saveUrl = rootPath + '/manage/examPaper/savePaperStudent/exec';
$(function(){
	brow = $.browser;
	if(brow.mozilla) {
		if(document.addEventListener){ 
			document.addEventListener('DOMMouseScroll',scrollFunc,false); 
		}
	}
	if(brow.mozilla) {
		$('#oCounter').html((parseInt($('#picSizeCount').val())+parseInt(3))+ '0%');
	}
	
	//初始化加载页面
	if(multiple > 0){
		var cou = 10 - multiple - 2;
		if(cou < 2){
			cou = 2;
		}
		picResize(cou);
	}
	
	//计算悬浮层位置
	getWindowAttr();
	
	$('.box').css("width",window.screen.width-230);
	$('#ksbh_input').focus();
	$('#ksbh_input').bind('keypress',function(event){
		flag = 0;
		if(event.keyCode == "13") {
			onSubmit();
        }
    });
	
	$('.btn-submit').click(function(){
		onSubmit();
	});
	
	//缩小事件
	$('.fa-minus').click(function(){
		var picSizeCount = $('#picSizeCount').val();
		count = --picSizeCount;
		if(count<2){
			count=2;
		}
		picResize(count); 
		$('#picSizeCount').val(count);
	});
	//放大事件
	$('.fa-plus').click(function(){
		count = parseInt($('#picSizeCount').val()) + 1;
		if(count > 20){
			count=20;
		}
		picResize(count);
		$('#picSizeCount').val(count);
	});

	//首页事件
	$('.btn-first').click(function(){
		toPage(1);
	});
	//上一页事件
	$('.btn-previous').click(function(){
		toPage(previousPage);
	});
	//下一页事件
	$('.btn-next').click(function(){
		toPage(nextPage);
	});
	//尾页事件
	$('.btn-last').click(function(){
		toPage(total);
	});
	
	//焦点离开校验
	$("#ksbh_input").blur(function(){
		checkFun();
	});
});

function toPage(current){
	location.href= listUrl + 'current='+current+'&batchId='+batchId+'&gradeNo='+gradeNo+'&courseNo='+courseNo;
}

//校验方法
function checkFun(){
	var kh = $('#ksbh_input').val();//获取考号
	//输入值空值判断
	if(kh ==''){
		$('#ksbh_input').focus();
		flag=1;
		layer.msg('考生编号不能为空！');
		return false;
	}
}

//提交按钮保存事件
function onSubmit(){
	checkFun();
	if(flag==1){
		return false;
	}
	exeSave();
}

//执行保存
function exeSave(){
	var sfqk = $('input[name="sfqk"]:checked').val();
	if(sfqk =='1'){
		sfqk = 3;
	}else{
		skqk = 1;
	}
	common_ajax.ajax(saveUrl, {
		batchId : batchId,
		gradeNo : gradeNo,
		courseNo : courseNo,
		detailId : id,
		fileNameLike : fileName,
		studentNo : $('#ksbh_input').val(),
		sfqk : skqk
	}, 'json', function(data){
		if(data){
			if(data.resultCode=='000000'){
				layer.msg('操作成功！');
				setTimeout(function(){
					if(total == 1){
						toPage(1);
					}else if(current == total){
						toPage(current - 1);
					}else {
						toPage(current);
					}
				},1000);
			}else{
				//保存失败
				layer.msg('操作失败！',{icon:5});
			}
		}
	});
}

//鼠标滚轮放大图片事件
function zoomPicture(){
	picResize(counting());
	return false;
}

function counting(){
	count = $('#picSizeCount').val();
	if (event.wheelDelta >= 120) {
		if(count>19){
			count=19;
		}
		count++;
	}else if (event.wheelDelta <= -120) {
		if(count<3){
			count=3;
		}
		count--; 
	}
	return count;
}

function picResize(count){
	var widthImg = $('#oImage').width();
	var heightImg = $('#oImage').height();
	var bl = parseFloat(widthImg/heightImg);//比例
	
	if(brow.mozilla) {
	   var prePicSizeCount = $('#picSizeCount').val();//上次的数值
	   if(count > 2 && count < 11){
		   if(prePicSizeCount < count){ //变大
			   widthImg += 100;
			   heightImg += 100 / bl;
		   }else{ //缩小
			   widthImg -= 100;
			   heightImg -= 100 / bl;
		   }
		   $('#oCounter').html((parseInt(count)+parseInt(3)) + '0%');
	   }
	   
	   $('#oImage').attr("width",widthImg+"px").attr("height",heightImg+"px");
	   $('#oImage1').attr("width",widthImg+"px").attr("height",heightImg+"px");
	}else{
		$('#oImage').attr("style",'zoom:'+count+'0%');
		   $('#oImage1').attr("style",'zoom:'+count+'0%');
		   $('#oCounter').html(count + '0%');
	}
	
	$('#picSizeCount').val(count);
}

$('.tb-booth1').hover(function(e){
	$('.one-position-oper1').show();
	e.stopPropagation();
},function(){
	$('.one-position-oper1').show();
});

function changeImgSize(){
	var height = $('#oImage').height();
	$('.tb-booth1').attr("style","height:"+ (parseInt(height)+parseInt(30))+'px');
	$('.one-position-oper1').attr("style","margin-top:0px");
}

//页面窗口大小变化后重载事件
window.onresize = function () {
	getWindowAttr();
}

function getWindowAttr(){
	var win_width = $(window).width();
	var win_height = $(window).height();
	var div_widht = 260;
	var div_height = $(".pinned").height();
	
	var height = 0;
	if(win_height<div_height){
		height = 10;
	}else{
		if((win_height-div_height)/2<100){
			height = 10;
		}else{
			height = (win_height-div_height)/2-80;
		}
	}
	var width = (win_width-div_widht-25);
	$('.pinned').css("margin-left",width+"px").css("margin-top",0+"px");
}

/*********************** 
* 函数：判断滚轮滚动方向 
* 作者：walkingp 
* 参数：event 
* 返回：滚轮方向 1：向上 -1：向下 
*************************/ 
var scrollFunc=function(e){ 
	e=e || window.event; 
	if(e.detail){//Firefox
		var count = $('#picSizeCount').val();
		if(e.detail<=-3){
			if(count<3){
				count=3;
			}
			count--;
		}else if(e.detail>=3){
			if(count>19){
				count=19;
			}
			count++;
		}
		picResize(count);
		return false;
	}
}
